Setting up Hanami and Postgres with Docker Compose 您所在的位置:网站首页 docker-compose up Setting up Hanami and Postgres with Docker Compose

Setting up Hanami and Postgres with Docker Compose

2023-04-17 18:43| 来源: 网络整理| 查看: 265

Recently, I started delving into the Hanami framework which version 2 had been released a couple of months ago. Since I got to know Docker and Docker Compose, I use them over and over again because they help with running varied services on my machine without installing required dependencies, etc. If you don’t know them yet, I really encourage you to become acquainted with them. I find them highly convenient and I would like to use them also for setting API service connected with a relational database, in my case, it will be PostgreSQL. Let’s get the show on the road.

First of all, we have to make sure that the ruby version that we use is at least in version 3.0.0. That requirement is constituted by Hanami. To begin, we have to install the hanami gem.

gem install hanami

Once it’s done, let’s create a new Hanami application from the scratch.

hanami new simple-api

This process created a bunch of files inside of the directory that follows the name of applications that we provided in the previous command. In my case it’s simple_api. Now, we need to create Dockerfile to specify the runtime environment for our application.

# simple_api/Dockerfile.dev FROM ruby:3.2.1-alpine WORKDIR /usr/src/app COPY Gemfile Gemfile.lock ./ RUN apk update && apk add --no-cache build-base ruby-dev RUN bundle install EXPOSE 2300 COPY . .

We are going to use ruby:3.2.1-alpine image to avoid using an overloaded ruby image but there is required to install two libraries to make it work: build-base and ruby-dev. If you are familiar with Rails, you might know the port number 3000 which is widely used, in the case of Hanami applications the default port is 2300. Once the Dockerfile is ready, we can build an image

docker build -t simple-api -f Dockerfile.dev .

Everything looks good, so we can jump into a docker-compose file.

# simple_api/docker-compose.yaml version: '3.8' volumes: postgres-data: services: db: image: postgres volumes: - postgres-data:/var/lib/postgresql/data env_file: .env app: image: simple-api command: sh -c "hanami server" ports: - "2300:2300" depends_on: - db env_file: .env

What we specified here is the db service that is PostgreSQL database and our web application server - app. Image naming is pretty straightforward, as you could notice ports as well. We are going to use the default port number. In addition, we have to add POSTGRES_PASSWORD environment variable to the .env file, in my case: POSTGRES_PASSWORD=XYZ123QWE.

# simple_api/.env POSTGRES_PASSWORD=XYZ123QWE

Let’s run our containers by running

docker-compose up

Awesome, everything looks good but there is still one important thing to do, we have to connect simple-api web server with postgres. In the beginning, let’s add a few gems to the Gemfile.

# simple_api/Gemfile gem "rom", "~> 5.3" gem "rom-sql", "~> 3.6" gem "pg"

Next we have to add postgresql-dev library to the Dockerfile.dev file so that it can successfully install the pg gem. The line from the file should look like below:

RUN apk update && apk add --no-cache build-base ruby-dev postgresql-dev

Let’s stop containers, rebuild the image, and run containers again. Before we start connecting the API application with the database server, we can create a database. We have to connect to psql on the database container and call SQL statement, so:

docker-compose exec db psql -U postgres CREATE DATABASE simple_api_development;

Now it’s time to get a closer look at Hanami framework itself because we have to add a persistence provider. We are supposed to create the following file:

# simple_api/config/providers/persistence.rb Hanami.app.register_provider :persistence, namespace: true do prepare do require "rom" config = ROM::Configuration.new(:sql, target["settings"].database_url) register "config", config register "db", config.gateways[:default].connection end start do config = target["persistence.config"] config.auto_registration( target.root.join("lib/simple_api/persistence"), namespace: "SimpleAPI::Persistence" ) register "rom", ROM.container(config) end end

This target["settings"].database_url refers to an environment variable with DATABASE_URL name, let’s add DATABASE_URL=postgres://postgres:XYZ123QWE@db:5432/simple_api_development to the .env file.

# simple_api/.env DATABASE_URL=postgres://postgres:XYZ123QWE@db:5432/simple_api_development POSTGRES_PASSWORD=XYZ123QWE

Unfortunately, that’s not everything, we have to also add the database_url setting to the Settings class:

# simple_api/config/settings.rb module SimpleAPI class Settings


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有